<script>
	export default {
		name: "my-login"
	}
</script>
<script setup>
	import nuni from '../../static/utils/request.js'
	import {
		useUserStore
	} from '@/stores/user.js'
	const userStore = useUserStore()
	// 获取用户信息（基础库低版本）
	const getUserInfo = (e) => {
		if (e.detail.errMsg === 'getUserInfo:fail auth deny') return nuni.$showMsg('已取消授权')
		userStore.updateUIO(e.detail.userInfo)
		console.log(userStore.userInfoObj);
		getToken(e.detail)
	}
	const getToken = async info => {
		// 获取临时登录凭证
		const res = await uni.login()
		if (res.errMsg !== 'login:ok') return nuni.$showMsg('登录失败！')
		console.log(res.code);
		console.log(info);
		// 调用登录接口
		const res2 = await nuni.request({
			url: '/api/public/v1/users/wxlogin',
			method: 'POST',
			data: {
				code: res.code,
				...info
			}
		})
		// 被迫写死
		const fakeToken =
			'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
		userStore.updateToken(fakeToken)
		redirectToBeforeLogin()
	}
	// 重定向跳转函数
	const redirectToBeforeLogin = () => {
		if (!userStore.backRedirectInfo) return;
		if (userStore.backRedirectInfo.openType === 'switchTab')
			uni.switchTab({
				url: userStore.backRedirectInfo.from,
				complete: () => {
					userStore.updateBackRedirectInfo(null)
				}
			})
	}
</script>
<template>
	<!-- <view style="height: 750px; background-color: white;"> -->
	<view class="login-ctr">
		<uni-icons type="contact-filled" size="100" color="#afafaf" class="icon-no-login"></uni-icons>
		<button type="primary" class="login-btn" @getuserinfo="getUserInfo" open-type="getUserInfo">一键登录</button>
		<text class="tip-text">登录后尽享更多服务</text>
	</view>
	<!-- 	</view> -->
</template>


<style lang="scss">
	.login-ctr {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 750rpx;
		background-color: #f5f5f5;
		position: relative;
		overflow: hidden;

		&::after {
			content: '';
			display: block;
			width: 100%;
			height: 40px;
			background-color: #fff;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translate(0, 50%);
		}

		.login-btn {
			width: 90%;
			border-radius: 100px;
			margin: 10px 0;
			background-color: #C00000;
		}

		.tip-text {
			font-size: 12px;
			color: gray;
		}
	}
</style>